<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Yun;

use Kcdns\Admin\Controller\PublicController;
class Yun extends \Service\Common\BaseModel
{
    protected $name = 'warehouse_yun';

    
    public function deliver($id,$data)
    {
    	$data = OE('Util')->validate($data, [
    			'warehouse_yun_order.status' => 'required;label=订单状态',
    			'warehouse_yun_order.express_company' => 'required;label=快递公司',
    			'warehouse_yun_order.express_no' => 'required;label=快递单号',
    			]);
    	$company_list = ["顺丰速递","圆通快递","中通快递","申通快递","韵达快递","国通快递","百世汇通","EMS","自提"];
    	$express_company = $company_list[$data['warehouse_yun_order.express_company']];
    	$update = M('warehouse_yun_order')->where([
    			           'id' => $id,
    			           'status' => 0
    			         ])->save([
    			         	'status' => $data['warehouse_yun_order.status'],
    			         	'express_company' => $express_company,
    			         	'express_no' => $data['warehouse_yun_order.express_no'],
    			         	'express_time' => date('Y-m-d H:i:s')
    			         	]);
    	if($update === false){
    		throw new \Exception('发货失败');
    	}
    }
   
    public function add_num($data)
	{
		$data = OE('Util')->validate($data, [
				'username' => 'required;label=用户名',
				'goods_id' => 'required;label=商品id',
				'buy_num' => 'required;price;label=购买数量',
				]);
		$usernfo = $this->queryUserInfo($data['username']);
		if (!$usernfo) {
			throw new \Exception('用户名错误');
		}
		$goods_list = M('goods')->where([
				'id'=>$data['goods_id'],
				'status'=>1
				])->find();
		if (!$goods_list) {
			throw new \Exception('无效的商品id');
		}
		$result = $this->where([
				'uid'=>$usernfo['uid'],
				'goods_id'=>$data['goods_id']
				])->find();
		if ($result){
			$update = $this->where([
					   'id'=>$result['id']
					])->save([
					   'stock' => ['exp','stock+'.$data['buy_num']],
					   'stock_money' => ['exp','stock_money+'.$data['buy_num'] * $goods_list['sell_price']],
					   'update_tme' => date('Y-m-d H:i:s')
					]);
			if ($update === false) {
				throw new \Exception('更新失败');
			}
		}else{
			$insert_id = $this->add([
					   'uid'=>$usernfo['uid'],
					   'goods_id' => $goods_list['id'],
					   'price' => $goods_list['sell_price'],
					   'stock' => $data['buy_num'],
					   'stock_money' => $data['buy_num'] * $goods_list['sell_price'],
					   'status' => 1,
					   'create_time' => date('Y-m-d H:i:s')
					]);
			if (!$insert_id) {
				throw new \Exception('添加失败');
			}
		}
		$add_id = M('warehouse_yun_log')->add([
				     'goods_id' => $goods_list['id'],
				     'uid' => $usernfo['uid'],
				     'type' => 0,
				     'num' => $data['buy_num'],
				     'create_time' => date('Y-m-d H:i:s')
				]);
		if (!$add_id) {
			throw new \Exception('添加失败');
		}
		return true;
	}
	
	public function getRealName($username)
	{
		$list = $this->queryUserInfo($username);
		if (!$list) {
			return false;
		}
		return $list['real_name'].'('.$list['mobile'].')';
	}
    
	public function queryUserInfo($username)
	{
		$list = M('user')->field('a.uid,b.mobile,a.real_name')
						->alias('a')
						->join('__UCENTER_MEMBER__ b on b.id = a.uid')
						->where(['b.username'=>$username])
						->find();
		if (!$list) {
			return false;
		}
		return $list;
	}

	public function yunList($uid, $page_num, $page_size){
		$where = [
			'uid' => $uid,
		];
		$list = (array)$this->where($where)->order('id desc')->page($page_num, $page_size)->select();
		foreach ($list as $key => &$row) {
			$row['title'] = M('goods')->where(['id' => $row['goods_id']])->getField('title');
		}
		$count = $this->where($where)->count();
		return [
			$list,
			$page_count = $count,
			$page_total = ceil($count / $page_size)
		];
	}
}